package www.rlicker.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import www.rlicker.model.GroupResource;
import www.rlicker.model.SysRole;
import www.rlicker.mybatis.Criteria;
import www.rlicker.mybatis.PageResult;
import www.rlicker.service.IGroupResourceService;
import www.rlicker.service.ISysRoleService;
import www.rlicker.utils.BaseController;
import www.rlicker.utils.JsonResult;
import www.rlicker.utils.MessageConstant;
import www.rlicker.utils.StringUtil;

import com.alibaba.fastjson.JSONArray;

@Controller
@RequestMapping("/sysrole")
public class SysRoleController extends BaseController {

	@Autowired
	private ISysRoleService service;

	@Autowired
	private IGroupResourceService groupResourceService;

	private List<String> list2;

	@RequestMapping("/showlist")
	public String showlist(ModelMap map, HttpServletRequest request) {
		List<SysRole> list = service.queryPageList(null);
		map.put("list", list);
		SysRole sysRole = service.queryByRoleKey(getUserId(request));
		if (sysRole != null) {
			Criteria<GroupResource> params = new Criteria<GroupResource>();
			params.addParam("resourceIdList", getResourceId(sysRole.getResourceId()));
			params.addParam("parent_resource_id",new Long(request.getParameter("id")));
			params.addParam("status", 0);
			params.addParam("showType", 1);
			List<GroupResource> lists = groupResourceService.queryPageList(params);
            map.put("gpLists", JSONArray.toJSONString(lists));
		}
        map.put("id", request.getParameter("id"));
		return "role/roleList";
	}

	@RequestMapping("/querypage")
	@ResponseBody
	public PageResult queryPage(HttpServletRequest request) {
		Criteria<SysRole> param = new Criteria<SysRole>();
		if (!StringUtil.isNullOrBlank(request.getParameter("roleId"))) {
			param.addParam("role_id", request.getParameter("roleId"));
		}
		if (!StringUtil.isNullOrBlank(request.getParameter("status"))) {
			param.addParam("status", request.getParameter("status"));
		}
	    if (!StringUtil.isNullOrBlank(request.getParameter("pageNumber"))) {
	    	param.addParam("startIndex", Integer.parseInt(request.getParameter("pageNumber")));
	    }
	    if (!StringUtil.isNullOrBlank(request.getParameter("pageSize"))) {
	    	param.addParam("endPage", Integer.parseInt(request.getParameter("pageSize")));
	    }
		List<Map<String, Object>> list = service.queryPage(param);
		PageResult pageResult = new PageResult();
		pageResult.setTotal(service.queryPageCount(param));
		pageResult.setRows(list);
		return pageResult;
	}

	@RequestMapping("/detail")
	public String detail(String roleId,String resourceId, ModelMap map) {
		
		if (!roleId.equals("null")) {
			SysRole role = service.queryEntity(Integer.parseInt(roleId));
			map.put("role", role);
			list2 = getResourceId(role.getResourceId());
		}
		List<GroupResource> list = null;
		Criteria<GroupResource> param = new Criteria<GroupResource>();
		param.addParam("parent_resource_id", 0);
		param.addParam("status", 0);
		list = groupResourceService.queryPageList(param);
		if (!roleId.equals("null")) {
			for (int i = 0; i < list.size(); i++) {
				if (list2.contains(list.get(i).getResourceId().toString())) {
					list.get(i).setIsShow("1");
				}
			}
		}
		list.forEach(f -> {
			Criteria<GroupResource> params = new Criteria<GroupResource>();
			params.addParam("parent_resource_id", f.getResourceId());
			params.addParam("status", 0);
			List<GroupResource> lists = groupResourceService
					.queryPageList(params);
			if (!roleId.equals("null")) {
				for (int i = 0; i < lists.size(); i++) {
					if (list2.contains(lists.get(i).getResourceId().toString())) {
						lists.get(i).setIsShow("1");
					}
				}
			}
			f.setList(lists);
			if (lists.size() > 0) {
				lists.forEach(s -> {
					Criteria<GroupResource> paramse = new Criteria<GroupResource>();
					paramse.addParam("parent_resource_id", s.getResourceId());
					paramse.addParam("status", 0);
					List<GroupResource> listse = groupResourceService
							.queryPageList(paramse);
					if (!roleId.equals("null")) {
						for (int i = 0; i < listse.size(); i++) {
							if (list2.contains(listse.get(i).getResourceId()
									.toString())) {
								listse.get(i).setIsShow("1");
							}
						}
					}
					s.setLists(listse);
				});
			}
		});
		map.put("list", list);
		map.put("resourceId", resourceId);
		return "role/roleDetail";
	}

	@ResponseBody
	@RequestMapping("/delete")
	public JsonResult delete(Integer roleId,String roleName) {
		String ret = service.deleteById(roleId);
		JsonResult jr = JsonResult.newInstance();
		String content = "删除了"+roleName+"角色";
		insertLog(content);
		return ret == null ? jr.declareSuccess() : jr.declareFailure(ret);
	}

	@ResponseBody
	@RequestMapping(value = "/insert", method = { RequestMethod.POST })
	public JsonResult insert(HttpServletRequest request){
		JsonResult jr = JsonResult.newInstance();
		SysRole role = service.queryByName(request.getParameter("roleName"));
		if (role != null) { 
			return jr.declareFailure(MessageConstant.USER_TYPE_2);
	    }
		SysRole sysRole = new SysRole();
		sysRole.setRoleName(request.getParameter("roleName"));
		sysRole.setResourceId(request.getParameter("resourceId"));
		sysRole.setResourceName(request.getParameter("resourceName"));
		sysRole.setStatus(Integer.parseInt(request.getParameter("status")));
		sysRole.setCreateTime(new Date());
		String ret = service.insert(sysRole);
		String content = "添加了"+request.getParameter("roleName")+"角色";
		insertLog(content);
		return ret == null ? jr.declareSuccess() : jr.declareFailure(ret);
	}

	@ResponseBody
	@RequestMapping(value = "/update", method = { RequestMethod.POST })
	public JsonResult update(SysRole role) {
		JsonResult jr = JsonResult.newInstance();
		Criteria<SysRole> param = new Criteria<SysRole>();
		param.setRecord(role);
		param.addParam("role_id", role.getRoleId());
		String ret = service.update(param);
		String content ="修改了"+role.getRoleName()+"角色";
		insertLog(content);
		return ret == null ? jr.declareSuccess() : jr.declareFailure(ret);
	}
	
	@ResponseBody
	@RequestMapping(value = "/updates", method = { RequestMethod.POST })
	public JsonResult updates(SysRole role) {
		JsonResult jr = JsonResult.newInstance();
	    String content = null;
		Criteria<SysRole> param = new Criteria<SysRole>();
		if (role.getStatus() == RETURN_JSONFIVE) {
		    content ="启用了"+role.getRoleName()+"角色";
		}else {
			content ="停用了"+role.getRoleName()+"角色";
		}
		param.setRecord(role);
		param.addParam("role_id", role.getRoleId());
		String ret = service.update(param);
		insertLog(content);
		return ret == null ? jr.declareSuccess() : jr.declareFailure(ret);
	}
}
